package com.woodbird.legend;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;

import com.woodbird.legend.configuration.Configuration;
import com.woodbird.legend.instrument.DefaultInstrument;
import com.woodbird.legend.instrument.Instrument;
import com.woodbird.legend.instrument.InstrumentException;

/**
 * Goal which touches a timestamp file.
 * 
 * @goal generate
 * @phase compile
 */
public class GenerateMojo extends AbstractMojo {

	public void execute() throws MojoExecutionException {
		getLog().info("Begin to generate calsses!");
		getLog().info("Excludes: "+joinPattern(excludes));
		getLog().info("Includes: "+joinPattern(includes));
		File baseDir =new File(project.getBuild().getOutputDirectory());;
		
		getLog().info("basedir:" + baseDir.getAbsolutePath());
		if(!baseDir.exists()){
			getLog().info("Base dir not exist!");
			return;
		}
		Instrument instrument = new DefaultInstrument(getLog(),Configuration.loadConfiguration());
		List<File> files;
		try {
			files = FileUtils.getFiles(baseDir, joinPattern(includes), joinPattern(excludes));
		} catch (IOException e) {
			throw new MojoExecutionException("Failed:", e);
		}
		for (File file : files) {
			if (file.getName().endsWith(".class")) {
				try {
					instrument.instrument(file);
				} catch (InstrumentException e) {
					getLog().info("Fail to generate class:" + file.getAbsolutePath());
				}
			}
		}
		this.getLog().info("Generate calsses successed!");
	}

	private String joinPattern(String[] patterns) {
		if (patterns.length <= 0)
			return null;
		StringBuilder sb = new StringBuilder();
		for (String str : patterns) {
			sb.append(str + ",");
		}
		if (sb.charAt(sb.length() - 1) == ',')
			sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	/**
	 * <i>Maven Internal</i>: Project to interact with.
	 * 
	 * @parameter expression="${project}"
	 * @required
	 * @readonly
	 */
	protected MavenProject project;

	/**
	 *@parameter expression="${legend.excludes}"
	 */
	protected String[] excludes;

	/**
	 *@parameter expression="${legend.includes}"
	 */
	protected String[] includes;
	
	/**
	 *@parameter expression="${legend.basedir}"
	 */
	protected String[] basedir;

}
